<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-7-18
 * Time: 下午7:46
 */

namespace ActionsHttp;

use Data\Retreat_food_DAO;
use Data\Order_form_DAO;
use Data\Order_for_foods_DAO;
use Data\Food_DAO;

class RestaurantRetreatFoodConfirm extends BaseActionHttp
{
    public function doAction()
    {
        parent::doAction();
        $_SESSION[RESTAURANT_ID] = 1;

        // 订单id
        $order_form_id = (int)$this->data[ORDER_FORM_ID];
        // 退单状态
        $retreat_status_res = (int)$this->data[RETREAT_STATUS];

        // 订单记录
        $order_form_record = Order_form_DAO::getRecord($order_form_id);

        if (!$order_form_record) {
            return $this->SendErrMsg("没有该订单记录");
        }
        if ($order_form_record->getRETREAT_FOOD_STATUS() != RETREAT_FOOD_STATUS_START) {
            return $this->SendErrMsg("没有该退菜申请");
        }
        if ($order_form_record->getIS_ENTRY() == STATE_YES) {
            return $this->SendErrMsg("该订单已入帐，不能退菜");
        }
        if ($order_form_record->getPAY_STATUS() != PAY_STATUS_YES) {
            return $this->SendErrMsg("订单未付款，不能退菜");
        }
        if ($order_form_record->getRETREAT_ORDER_STATUS() == RETREAT_ORDER_STATUS_START) {
            return $this->SendErrMsg("有退单申请进行中，不能退菜");
        }
        if ($order_form_record->getRETREAT_ORDER_STATUS() == RETREAT_ORDER_STATUS_SUC) {
            return $this->SendErrMsg("已退单，不能退菜");
        }
        // 商家id
        $restaurant_id = $order_form_record->getRESTAURANT_ID();
        if ($restaurant_id != $_SESSION[RESTAURANT_ID]) {
            return $this->SendSessionOut();
        }


        if ($retreat_status_res == RESTAURANT_REFUSE) {
            // 拒绝
            try {
                // 事务 开始
                $dbMain = \GatewayWorker\Lib\Db::instance('main');
                $dbMain->beginTrans();

                $order_form_record->setRETREAT_FOOD_STATUS(RETREAT_FOOD_STATUS_REFUSE);
                Order_form_DAO::update($order_form_record);
                // 订单中商品信息
                $order_for_food_list = Order_for_foods_DAO::getRetreattingLis($order_form_id);
                foreach ($order_for_food_list as $order_for_food_record) {
                    $order_for_food_record->setRETREAT_STATUS(RETREAT_FOOD_STATUS_REFUSE);
                    Order_for_foods_DAO::update($order_for_food_record);
                }

                //事务 提交
                $dbMain->commitTrans();

            } catch (\Exception $ex) {
                //出现异常,回滚
                echo "预料外错误~~~~\n";
                echo $ex->getMessage() . "\n";
                echo $ex->getTraceAsString() . " ~~~ traceString\n";
                echo $ex->getCode() . "  ~~code  \n";
                echo $ex->getFile() . " ~~ file \n";
                echo $ex->getLine() . " ~~ line \n";
                echo $ex->getPrevious() . " ~~ previous \n";
                $dbMain->rollBackTrans();
                // 预料外的错误
                return $this->SendErrMsg("预料外的错误~!");
            }

        } elseif ($retreat_status_res == RESTAURANT_CONFIRM) {
            // 同意

            try {
                // 事务 开始
                $dbMain = \GatewayWorker\Lib\Db::instance('main');
                $dbMain->beginTrans();

                $order_form_record->setRETREAT_FOOD_STATUS(RETREAT_FOOD_STATUS_SUC);
                Order_form_DAO::update($order_form_record);
                // 订单中商品信息
                $order_for_food_list = Order_for_foods_DAO::getRetreattingLis($order_form_id);
                foreach ($order_for_food_list as $order_for_food_record) {
                    $food_count_left = $order_for_food_record->getFOOD_COUNT_LEFT() - $order_for_food_record->getRETREAT_COUNT();
                    if ($food_count_left < 0) {
                        return $this->SendErrMsg("错误，退菜信息不符");
                    }
                    $order_for_food_record->setFOOD_COUNT_LEFT($food_count_left);
                    $order_for_food_record->setSUC_RETREAT_COUNT($order_for_food_record->getSUC_RETREAT_COUNT() + $order_for_food_record->getRETREAT_COUNT());
                    $order_for_food_record->setSUC_RETREAT_MONEY($order_for_food_record->getSUC_RETREAT_MONEY() + $order_for_food_record->getRETREAT_MONEY());
                    $order_for_food_record->setRETREAT_STATUS(RETREAT_FOOD_STATUS_SUC);
                    Order_for_foods_DAO::update($order_for_food_record);
                }

                //事务 提交
                $dbMain->commitTrans();

            } catch (\Exception $ex) {
                //出现异常,回滚
                echo "预料外错误~~~~\n";
                echo $ex->getMessage() . "\n";
                echo $ex->getTraceAsString() . " ~~~ traceString\n";
                echo $ex->getCode() . "  ~~code  \n";
                echo $ex->getFile() . " ~~ file \n";
                echo $ex->getLine() . " ~~ line \n";
                echo $ex->getPrevious() . " ~~ previous \n";
                $dbMain->rollBackTrans();
                // 预料外的错误
                return $this->SendErrMsg("预料外的错误~!");
            }

            // 返回数据
            $this->ResponseDataToCurrentClient();
        }
    }

}